package gameengine.com.state;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import resolution.DEF;

import android.R.color;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.KeyEvent;

import gameengine.com.BitmapFont;
import gameengine.com.Dialog;
import gameengine.com.GameLayer;
import gameengine.com.GameLib;
import gameengine.com.IConstant;
import gameengine.com.MainActivity;
import gameengine.com.Map;
import gameengine.com.actor.Creature;
import gameengine.com.actor.Explorer;

public class StateMainMenu extends MainActivity implements IConstant
{

	public static Bitmap splashBitmap = null;
	public static final int MENU_PLAY = 0;
	public static final int MENU_HOW_PLAY = 1;
	public static final int MENU_OPTION = 2;
	public static final int MENU_CREDIT = 3;
	public static final int MENU_EXIT = 4;
	public static int[] arrayMenu = { MENU_PLAY, MENU_HOW_PLAY, MENU_OPTION, MENU_CREDIT, MENU_EXIT };
	public static String[] arrayMenuString = { "PLAY", "HOW TO PLAY", "OPTION", "CREDIT", "EXIT" };
	public static int MENU_BEGIN_X = 0; //will iit in contructor
	public static int MENU_BEGIN_Y = 200;
	public static int MENU_ELEMENT_W = 0; //will iit in contructor
	public static int MENU_ELEMENT_H = 0; //will iit in contructor
	public static int MENU_ELEMENT_SPACE = 20;
	public static int MENU_H = SCREEN_HEIGHT;

	public static synchronized void SendMessage(int type)
	{

		switch (type)
		{
		case MESSAGE_CTOR:

			if (splashBitmap == null) 
				splashBitmap = loadImageFromAsset("image/splash.png");
			MENU_ELEMENT_H = StateGameplay.spriteDPad.getFrameHeight(DEF.FRAME_BUTTON_NORMAL);
			MENU_ELEMENT_W = StateGameplay.spriteDPad.getFrameWidth(DEF.FRAME_BUTTON_NORMAL);
			MENU_BEGIN_X = SCREEN_WIDTH - MENU_ELEMENT_W/2 - 50;
			MENU_H = (MENU_ELEMENT_SPACE+ MENU_ELEMENT_H)*arrayMenu.length;
			MENU_BEGIN_Y = (SCREEN_HEIGHT - MENU_H)/2 +20;
			StateGameplay.isIngame  = false;
			break;
		case MESSAGE_UPDATE:

			int temp = -1;
			if (Dialog.isShowDialog) {
				Dialog.updateDialog();
				return;
			} 
				for (int i = 0; i < arrayMenu.length; i++) {
					int y = MENU_BEGIN_Y + i * (MENU_ELEMENT_H + MENU_ELEMENT_SPACE);
					if (isTouchReleaseInRect(MENU_BEGIN_X - MENU_ELEMENT_W / 2, y - MENU_ELEMENT_H / 2, MENU_ELEMENT_W, MENU_ELEMENT_H)) {

						switch (i)
						{
						case MENU_PLAY:
							changeState(STATE_SELECT_LEVEL);
							break;
						case MENU_OPTION:
							changeState(STATE_OPTION);
							break;							
							
						case MENU_HOW_PLAY:
							changeState(STATE_HOW_TO_PLAY);
							break;
						case MENU_CREDIT:
							changeState(STATE_CREADIT);
							break;										
						case MENU_EXIT:
							Dialog.showDialog(Dialog.DIALOG_TYPE_CONFIRM, "","Ban co chac muon thoat game ko?",Dialog.DIALOG_NEXTSTATE_EXIT);
							break;
						default:
							break;
						}
					}
				}
			
			break;
		case MESSAGE_PAINT:

			MainActivity.mainPaint.setColor(Color.BLACK);
			MainActivity.mainPaint.setStyle(Style.FILL);
		//	canvasScreenBuffer
			matrix.reset();
			canvasScreenBuffer.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, MainActivity.mainPaint);					
			canvasScreenBuffer.drawBitmap(splashBitmap, matrix, mainPaint);
			matrix.setScale(SCREEN_WIDTH * 1f / 800, SCREEN_HEIGHT * 1f / 1280);
			mainCanvas.drawBitmap(bitmapScreenBuffer, matrix, mainPaint);
			//		mainCanvas.drawText(strString, 000, 100, mainPaint);
			if (Dialog.isShowDialog) {
				Dialog.drawDialog(mainCanvas);
				return;
			} 
				for (int i = 0; i < arrayMenu.length; i++) {
					int y = MENU_BEGIN_Y + i * (MENU_ELEMENT_H + MENU_ELEMENT_SPACE);
					if (isTouchDrapInRect(MENU_BEGIN_X - MENU_ELEMENT_W / 2, y - MENU_ELEMENT_H / 2, MENU_ELEMENT_W, MENU_ELEMENT_H))
						StateGameplay.spriteDPad.drawAFrame(mainCanvas, DEF.FRAME_BUTTON_HIGHTLIGHT, MENU_BEGIN_X, y);
					else
						StateGameplay.spriteDPad.drawAFrame(mainCanvas, DEF.FRAME_BUTTON_NORMAL, MENU_BEGIN_X, y);
					fontWhite.drawString(mainCanvas, arrayMenuString[i], MENU_BEGIN_X, y - 18, BitmapFont.ALIGN_CENTER);
				}
			
			//if(System.currentTimeMillis()%1000 > 500)
			//if (fontbig_White != null)
			//	fontbig_White.drawString(mainCanvas, "TOUCH THE SCREEN TO CONTINUE", SCREEN_WIDTH/2, SCREEN_HEIGHT - 150, BitmapFont.ALIGN_CENTER);	

			break;
		case MESSAGE_DTOR:
			break;
		}
	}
}
